/*
 * @FileName: OrganizationService.java
 * 
 * @Author
 * 
 * @Description:
 * 
 * @Date 2016年12月26日 下午2:42:39
 * 
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.model.SysCodeType;

/**
 * Class description goes here
 *
 * @author
 */
public class SysCodeTypeService{
    private static final Logger      logger  = LoggerFactory.getLogger(SysCodeTypeService.class);
    public static SysCodeTypeService service = new SysCodeTypeService();

    /**
     * @Title: getProfileListByGroupId
     * @Description:TODO
     * @param serviceType
     * @param groupId
     * @return List<Record>
     */
    public List<Record> getProfileListByGroupId(String serviceType, String groupId){
        List<Record> list = SysCodeType.dao.getCodeValueByTypeCode(serviceType);

        if(null != list){
            return toNewList(list, groupId);
        }

        return null;
    }

    /**
     * @Title: toNewList
     * @Description:TODO
     * @param list
     * @param groupId
     * @return List<Record>
     */
    private List<Record> toNewList(List<Record> list, String groupId){
        List<Record> newList = new ArrayList<Record>();

        for(Record r : list){
            if(null != r && isInGroup(r, groupId)){
                newList.add(r);
            }
        }

        return newList;
    }

    /**
     * @Title: isInGroup
     * @Description:TODO
     * @param r
     * @param groupId
     * @return boolean
     */
    private boolean isInGroup(Record r, String groupId){
        String groupIds = r.getStr("remark");// 保存的groupId串，以“,”隔开
        if(StrKit.notBlank(groupIds) && StrKit.notBlank(groupId)){// remark字段不为空表示指定地市可见
            String groupIdArr[] = groupIds.split(",");
            if(Arrays.asList(groupIdArr).contains(groupId)){
                return true;
            }
        } else if(StrKit.isBlank(groupIds)){// remark字段为空表示全省可见
            return true;
        }
        return false;
    }

}
